# Maintainer: Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
# Contributor: Iwan Timmer <irtimmer@gmail.com>
# Contributor: Mark Constable <markc@renta.net>
# Contributor: Anatol Pomozov <anatol.pomozov@gmail.com>

pkgbase=cockpit
pkgname=(cockpit cockpit-packagekit cockpit-pcp cockpit-storaged)
pkgver=290
pkgrel=1
pkgdesc='A systemd web based user interface for Linux servers'
arch=('loong64' 'x86_64')
url='https://cockpit-project.org/'
license=(LGPL)
makedepends=(krb5 libssh accountsservice perl-json perl-locale-po json-glib glib-networking
             git intltool gtk-doc gobject-introspection networkmanager libgsystem xmlto npm pcp
             gendesk)
source=("https://github.com/cockpit-project/cockpit/releases/download/$pkgver/cockpit-$pkgver.tar.xz"
        "cockpit.pam"
        "cockpit-ws.sysuser.conf"
        "cockpit-wsinstance.sysuser.conf")
sha256sums=('0b67c26f436772975c03f5c16b0c57f2e0a318165d847bab0d2cc5e1a9237f1b'
            'a979e236681c6a06906937cf0f012e976347af5d6d7e7ae04a11acb01cc2689d'
            '1ad9dad75858264778bd94799b60c651f7cc1c7f7fa1c54622174303e639287a'
            '46ee8ecad7bc97ba588ab9471dde76e41c00daf40658902425626c3a1938b438')

prepare() {
  cd cockpit-$pkgver
  # disable buggy package-lock check
  sed -r '/^cmd_make_package_lock_json\b/ a exit 0' -i tools/node-modules
}

build() {
  cd cockpit-$pkgver
  ./configure \
    --prefix=/usr \
    --sbindir=/usr/bin \
    --libexecdir=/usr/lib/$pkgname/ \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --disable-dependency-tracking \
    --disable-silent-rules \
    --with-cockpit-user=cockpit-ws \
    --with-cockpit-ws-instance-user=cockpit-wsinstance
  make all

  gendesk -n \
    --name 'Cockpit Client' \
    --comment 'Connect via ssh to servers running Cockpit' \
    --exec '/usr/lib/cockpit/cockpit-desktop /' \
    --categories 'Network;RemoteAccess' \
    --custom 'DBusActivatable=true' \
    --icon cockpit-client \
    --pkgname cockpit
}

package_cockpit() {
  depends=(krb5 libssh accountsservice perl-json perl-locale-po json-glib glib-networking
           libpwquality openssl kexec-tools which)
  backup=('etc/pam.d/cockpit')
  optdepends=("cockpit-pcp: reading performance metrics"
              "cockpit-storaged: manage storage"
              "cockpit-packagekit: manage packaged"
              "cockpit-podman: user interface for managing podman containers"
              "cockpit-machines: user interface for managing virtual machines"
              "polkit: elevate privileges"
              "sudo: elevate privileges"
              "networkmanager: manage network connections"
              "sssd: authentication"
              "sscg: generate self-signed certificate")

  cd cockpit-$pkgver
  make DESTDIR="$pkgdir" install
  rm -rf "$pkgdir"/usr/{src,lib/firewalld}
  install -Dm644 "$srcdir"/cockpit.pam "$pkgdir"/etc/pam.d/cockpit
  install -Dm644 "$srcdir"/cockpit-ws.sysuser.conf "$pkgdir"/usr/lib/sysusers.d/cockpit-ws.conf
  install -Dm644 "$srcdir"/cockpit-wsinstance.sysuser.conf "$pkgdir"/usr/lib/sysusers.d/cockpit-wsinstance.conf

  echo "z /usr/lib/cockpit/cockpit-session - - cockpit-wsinstance -" >> "$pkgdir"/usr/lib/tmpfiles.d/cockpit-tempfiles.conf

  install -Dm644 cockpit.desktop "$pkgdir"/usr/share/applications/org.cockpit_project.CockpitClient.desktop

  # remove unused plugins
  rm -rf "$pkgdir"/usr/share/cockpit/{selinux,playground,sosreport} \
         "$pkgdir"/usr/share/metainfo/org.cockpit-project.cockpit-{selinux,sosreport}.metainfo.xml

  # remove plugins packaged separately
  rm -rf "$pkgdir"/usr/share/cockpit/{apps,packagekit,pcp,storaged} \
         "$pkgdir"/usr/share/metainfo/org.cockpit-project.cockpit-storaged.metainfo.xml \
         "$pkgdir"/usr/lib/cockpit/cockpit-pcp \
         "$pkgdir"/var/lib/pcp
}

package_cockpit-pcp() {
  pkgdesc='Cockpit support for reading PCP metrics and loading PCP archives'
  depends=(cockpit pcp)

  cd cockpit-$pkgver
  make DESTDIR="$pkgdir"/tmp install

  cd "$pkgdir"/tmp
  bsdtar -cf - usr/share/cockpit/pcp usr/lib/cockpit/cockpit-pcp var/lib/pcp \
    | bsdtar -xf - -C "$pkgdir"
  rm -rf "$pkgdir"/tmp
}

_do_package_component() {
  _component="${1:-${pkgname#cockpit-}}"

  cd "$srcdir"/cockpit-$pkgver
  make DESTDIR="$pkgdir"/tmp install

  cd "$pkgdir"/tmp
  bsdtar -cf - usr/share/cockpit/$_component \
    | bsdtar -xf - -C "$pkgdir"

  [ -f usr/share/metainfo/org.cockpit-project.$pkgname.metainfo.xml ] && \
    install -Dm644 usr/share/metainfo/org.cockpit-project.$pkgname.metainfo.xml \
      "$pkgdir"/usr/share/metainfo/org.cockpit-project.$pkgname.metainfo.xml

  rm -rf "$pkgdir"/tmp
}

package_cockpit-storaged() {
  pkgdesc='Cockpit user interface for storage, using udisks'
  depends=(cockpit udisks2 dbus-python)
  optdepends=(
    "clevis: manage disk encryption"
  )
  _do_package_component
}

package_cockpit-packagekit() {
  pkgdesc='Cockpit user interface for packages'
  depends=(cockpit polkit packagekit python)
  _do_package_component
  _do_package_component apps
}
